package at.fhv.androideightpuzzle.network.messageparsers;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import at.fhv.androideightpuzzle.network.message.IMessage;
import at.fhv.androideightpuzzle.network.message.PuzzleSolvedMessage;

public class PuzzleSolvedMessageParser implements IMessageParser{
	
	public PuzzleSolvedMessageParser(){
		//
	}
	
	public IMessage parseMessage(String message){
		try{
			JSONParser parser=new JSONParser();
			JSONObject jsonObj=(JSONObject)parser.parse(message);
			
			return parseMessage(jsonObj);
		}catch(Exception exc){
			android.util.Log.e("PuzzleSolvedMessageParser",exc.getMessage(),exc);
			
			return null;
		}
	}
	
	public IMessage parseMessage(JSONObject jsonObj){
		try{
			JSONObject payload=(JSONObject) jsonObj.get("payload");
			
			String playerName=(String)payload.get("playername");
			long time=((Number)payload.get("time")).longValue();
			
			return new PuzzleSolvedMessage(playerName,time);
		}catch(Exception exc) {
			android.util.Log.e("PuzzleSolvedMessageParser",exc.getMessage(),exc);
			
			return null;
		}
	}
}
